/*
 * Copyright (c) 2011, Marc-Andre Ferland - madrang@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the The Warrent Team nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *     * This software is free for non-commercial use. You may not use this
 *       software, in whole or in part, in support of any commercial product
 *       without the express consent of the author.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE WARRENT TEAM BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Runtime.InteropServices;

namespace OsHelper.UsbNet
{
	/* Characteristics of request:
	 * D7: Data transfer direction
	 * Host-to-device = 0
	 * Device-to-host = 1
	 * 
	 * D6...5: Type
	 * Standard = 0
	 * Class = 1
	 * Vendor = 2
	 * Reserved = 3
	 * 
	 * D4...0: Recipient
	 * Device = 0
	 * Interface = 1
	 * Endpoint= 2
	 * Other = 3
	 * Reserved = [4,31]
	 */

	public enum TransferDirection : byte
	{
		HostToDevice = 0,
		DeviceToHost = 1
	}
	
	public enum Type : byte
	{
		Standard = 0,
		Class = 1,
		Vendor = 2,
		Reserved = 3
	}
	
	public enum Recipient : byte
	{
		Device = 0,
		Interface = 1,
		Endpoint = 2,
		Other = 3,
		//Reserved = [4, 31]
	}
	
	[StructLayout(LayoutKind.Sequential, Pack = 0)]
	public struct RequestAttributes
	{
		private byte by_Value;
		
		private RequestAttributes (byte Value)
		{
			this.by_Value = Value;
		}
		
		public RequestAttributes (Recipient MyRecipient, TransferDirection MyDirection, Type MyType)
		{
			this.Recipient = MyRecipient;
			this.Direction = MyDirection;
			this.Type = MyType;
		}
		
		public static explicit operator byte(RequestAttributes a)
		{
			return a.by_Value;
		}
		
		public static explicit operator RequestAttributes(byte a)
		{
			return new RequestAttributes (a);
		}
		
		public TransferDirection Direction {
			get {
				return (TransferDirection)(1 & (this.by_Value >> 7));
			}
			set {
				if(((byte)value & ~1) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.by_Value |= (byte)(((byte)value & 1) << 7);
			}
		}
		
		public Type Type {
			get {
				return (Type)(3 & (this.by_Value >> 5));
			}
			set {
				if(((byte)value & ~3) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.by_Value |= (byte)(((byte)value & 3) << 5);
			}
		}
		
		public Recipient Recipient {
			get {
				return (Recipient)(31 & this.by_Value);
			}
			set {
				if(((byte)value & ~31) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.by_Value |= (byte)((byte)value & 31);
			}
		}
		
	}
}
